//
//  DataBaseHandler.m
//  Atlantis
//
//  Created by 홍지성 on 14/12/30.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DataBaseHandler.h"
#import "Music.h"
#import "PlayViewController.h"

@implementation DataBaseHandler




+(DataBaseHandler *)shareInstance
{
    // static 代码只走一次
    //    // 设数据库的头指针   
    //    // 当第一次执行的时候会产生一个空指针
    static DataBaseHandler *handler = nil;
    // 对指针进行判断, 当第一次执行的时候创建一个对象

    if (handler == nil) {
         // 单例创建不要relese/autorelease  不能回收内存
        handler = [[DataBaseHandler alloc] init];
    }
    // 无论是创建的和已经存在的, 都在这里直接return出去;
    return handler;
}
- (BOOL)judgeResult:(int)result text:(NSString *)text
{
    if (result == SQLITE_OK) {
        NSLog(@"%@SUCCESS", text);
        return YES;
    }else{
        NSLog(@"%@DEFEAT", text);
        return NO;
    }
}

- (BOOL)openDB
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *dbPath = [path stringByAppendingPathComponent:@"DBfile.db"];
    NSLog(@"%@", path);
    int result = sqlite3_open([dbPath UTF8String], &dbpoint);
    return [self judgeResult:result text:@"打开"];
}

// 创建表
- (BOOL)createTable
{
    NSString *sql = @"create table Music(trackId text primary key, title text)";
    int result = sqlite3_exec(dbpoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result text:@"创建表"];
}


// 添加一条对象数据
- (BOOL)insertMusic:(NSDictionary *)dic
{
    NSString *sql = [NSString stringWithFormat:@"insert into Music values('%@', '%@')", [dic objectForKey:@"trackId"], [dic objectForKey:@"title"]];
    int result = sqlite3_exec(dbpoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result text:@"插入数据"];

    
    
}


// 删除一条学生对象数据
- (BOOL)deleteMusicWithId:(NSString *)number
{
    
    NSString *sql = [NSString stringWithFormat:@"delete from Music where trackId = '%@'", number];
    int result = sqlite3_exec(dbpoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result text:@"删除"];
    
    
    
}



//查询
- (NSMutableArray *)selectAll
{
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select *from Music";
    int result = sqlite3_prepare_v2(dbpoint, [sql UTF8String], -1, &stmt, NULL);
    //NSMutableArray *strArr = [NSMutableArray array];
    NSMutableArray *tempArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *trackIdChar = sqlite3_column_text(stmt, 0);
            NSString *trackId = [NSString stringWithUTF8String:(const char *)trackIdChar];
            
            
            const unsigned char *titleChar = sqlite3_column_text(stmt, 1);
            NSString *title = [NSString stringWithUTF8String:(const char *)titleChar];
            
//            
//            const unsigned char *labelIDChar = sqlite3_column_text(stmt, 2);
//            NSString *labelID = [NSString stringWithUTF8String:(const char *)labelIDChar];
            
            
//            NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:trackId, @"trackId", title, @"title", nil];
            
           Music *music = [[Music alloc] init];
            music.trackId = trackId;
            music.title = title;
            
            [tempArr addObject:music];
            
//            music.articles = tempArr;
//            [strArr addObject:music];
        }
    }
    sqlite3_finalize(stmt);
    return tempArr;
}

























@end
